<?php
/**
 * Created by PhpStorm.
 * User: lpy
 * Date: 2018/8/16
 * Time: 18:13
 */

namespace app\api\logic;
use think\api\Client;
use think\facade\Config;
use think\facade\Db;

class LotteryLogic {
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct() {
        $this->app_code = Config::get('app.app_code');
    }

    /*购买彩种*/
    public function lottery_buy($lottery_id = 'ssq') {
        //最新开奖结果
        $lottery = Db::name('lottery_log')->where('lottery_id', $lottery_id)->order('lottery_no desc')->value('lottery_res');

        $lotterys = explode(',', $lottery);
        $six      = 24;

        for ($ii = 0; $ii < 10; $ii++) {

            $lottery_res = '';

            //号码算法
            switch ($lottery_id) {
            case 'ssq':
                //每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1--33中选择；蓝色球号码从1--16中选择。
                //已经选号码
                $choose = [];
                for ($i = 1; $i < 99999; $i++) {
                    $key = count($choose);
                    if ($key == 6) {
                        break;
                    }
                    if ($key == 5) {

                        if ($six > 33) {
                            $num = rand(24, 33);
                        } else {
                            $num = $six;
                        }
                        $six++;

                    } elseif ($key == 4) {
                        $num = rand(15, 28);
                    } elseif ($key == 3) {
                        $num = rand(12, 25);
                    } elseif ($key == 2) {
                        $num = rand(10, 19);
                    } elseif ($key == 1) {
                        $num = rand(6, 14);
                    } elseif ($key == 0) {
                        $num = rand(1, 10);
                    }

                    if ($num == $lotterys[$key]) {
                        //$six = $six + 1;

                        continue;
                    }
                    if (in_array($num, $choose)) {
                        //$six = $six + 1;

                        continue;
                    } else {

                        if ($num < 10) {
                            $num_str  = (string) $num;
                            $num_str  = '0' . $num_str;
                            $choose[] = $num_str;
                        } else {
                            $choose[] = $num;
                        }

                    }

                }

                $choose      = $this->getpao($choose);
                $lottery_res = implode(',', $choose);
                //蓝球

                for ($i = 0; $i < 100; $i++) {
                    if ($lotterys[6] > 9) {
                        $blue = rand(1, 10);

                    } else {
                        $blue = rand(11, 16);
                    }

                    if ($blue == $lotterys[6]) {
                        continue;
                    }

                    break;
                }
                if ($blue < 10) {
                    $blue = (string) $blue;
                    $blue = '0' . $blue;
                }

                $lottery_res = $lottery_res . '@' . $blue;

                break;

            default:
                # code...
                break;
            }

            $data['lottery_id']   = $lottery_id;
            $data['lottery_res']  = $lottery_res;
            $data['create_time']  = time();
            $data['lottery_date'] = Date('Y-m-d');
            Db::name('lottery_bonus')->insert($data);
        }

        return true;
        //dump('购买成功');
    }
    /*中奖计算器*/
    public function bonus($lottery_id = 'ssq') {
        //记录开奖结果
        $client = new Client($this->app_code);

        $result = $client->lotteryQuery()
            ->withLotteryId($lottery_id)
            ->request();
        $lottery = Db::name('lottery_log')->where('lottery_id', $lottery_id)->order('id desc')->find();
        if ($lottery['lottery_no'] != $result['data']['lottery_no']) {
            $lottery_log['lottery_id']   = $lottery_id;
            $lottery_log['lottery_no']   = $result['data']['lottery_no'];
            $lottery_log['lottery_name'] = $result['data']['lottery_name'];
            $lottery_log['lottery_res']  = $result['data']['lottery_res'];
            $lottery_log['open_time']    = strtotime($result['data']['lottery_date']);
            Db::name('lottery_log')->insert($lottery_log);
        }

        $lotterys = Db::name('lottery_bonus')
            ->where('lottery_id', $lottery_id)
            ->where('status', 0)
            ->select()->toArray();
        if ($lotterys) {
            foreach ($lotterys as $key => $lottery) {
                $lottery_res = $lottery['lottery_res'];
                $client      = new Client($this->app_code);
                $result      = $client->lotteryBonus()
                    ->withLotteryId($lottery_id)
                    ->withLotteryRes($lottery_res)
                    ->request();
                $update_data = [];
                if (!empty($result['data']['lottery_prize'])) {
                    $update_data['prize_money']      = $result['data']['lottery_prize'][0]['prize_money'];
                    $update_data['lottery_date']     = $result['data']['lottery_date'];
                    $update_data['real_lottery_res'] = $result['data']['real_lottery_res'];
                    $update_data['prize_msg']        = $result['data']['lottery_prize'][0]['prize_name'];
                    $update_data['status']           = 1;
                    $update_data['prize_status']     = 1;
                    $update_data['bonus_time']       = time();
                    $update_data['lottery_no']       = $result['data']['lottery_no'];

                } else {

                    $update_data['lottery_date']     = $result['data']['lottery_date'];
                    $update_data['real_lottery_res'] = $result['data']['real_lottery_res'];
                    $update_data['prize_msg']        = '未中奖';
                    $update_data['status']           = 1;
                    $update_data['bonus_time']       = time();
                    $update_data['lottery_no']       = $result['data']['lottery_no'];
                }
                Db::name('lottery_bonus')->where('id', $lottery['id'])->update($update_data);

            }

        }
        return true;
        //dump('没有开奖');

    }
    function getpao($arr) {
        $len = count($arr);
        //设置一个空数组 用来接收冒出来的泡
        //该层循环控制 需要冒泡的轮数
        for ($i = 1; $i < $len; $i++) {
            //该层循环用来控制每轮 冒出一个数 需要比较的次数
            for ($k = 0; $k < $len - $i; $k++) {
                if ($arr[$k] > $arr[$k + 1]) {
                    $tmp         = $arr[$k + 1];
                    $arr[$k + 1] = $arr[$k];
                    $arr[$k]     = $tmp;
                }
            }
        }
        return $arr;
    }

}